import Web3 from 'web3';
import Uniprovider from "./uniprovider";

let rpc = "https://bsc-dataseed.binance.org:443/";
	rpc = "https://bsc-dataseed1.binance.org:443";
//const web3Rpc = new Web3(rpc);

const bip39 = require('bip39');
const hdkey = require('hdkey');
var util = require('ethereumjs-util');

export default {
	web3Init(){
		return new Web3(rpc);
	},
	async createMnemonicWords() {
	  let mnemonicWords = bip39.generateMnemonic();
	  console.log('createMnemonicWords', mnemonicWords)
	  return mnemonicWords;
	},
	async getPrivateKeyFromMnemonicWords(mnemonicWords) {
	  if (!mnemonicWords) {
	    throw new Error("Invalid mnemonic passed in");
	  }
	  let seed = await bip39.mnemonicToSeed(mnemonicWords);
	  /*  Use seed to generate HD wallet. If we want to be more specific,
	  we need to generate the master key and record it.*/
	  let hdWallet = hdkey.fromMasterSeed(seed);
	  let key1 = hdWallet.derive("m/44'/60'/0'/0/0");
	  let privateKey = '0x' + key1._privateKey.toString('hex');
	  console.log('word to privateKey', privateKey);
	  return privateKey
	},
	// Get public key
	getPublicKeyFromPrivateKey(privateKey) {
	  if (!privateKey) {
	    throw new Error("The private key passed in is invalid");
	  }
	  // kids hack
	  var check = privateKey.substring(0,2);
	  if(check.toLowerCase() != '0x'){
		  privateKey = '0x' + privateKey;
	  }
	  let publicKey = util.bufferToHex(util.privateToPublic(util.toBuffer(privateKey)));
	  console.log('publicKey', publicKey)
	  return publicKey;
	},
	// Get the address
	getAddressFromPublicKey(publicKey) {
	  //  Use the public key in keypair to generate the address.
	  var address = util.pubToAddress(util.toBuffer(publicKey), true);
	  //  Based on bip55 protocol, the address is encoded again to obtain the final eth address
	  address = util.toChecksumAddress('0x' + address.toString('hex')).toLowerCase();
	  console.log('address', address)
	  return address
	},
	
	
	/**
	 * Get account balance
	 * @param chainId
	 * @param address
	 * @returns {*}
	 *
	 */
	async getAccount(chainId, address) {
	  let rpc = getRpcUrl(chainId);
	  let currentProvider = new Uniprovider(rpc);
	  let provider = new ethers.providers.Web3Provider(currentProvider);
	  const balance = await provider.getBalance(address);
	  return balance.toString();
	},
	
	async ethGetNonce(chainId, address) {
	  let rpc = getRpcUrl(chainId);
	  let currentProvider = new Uniprovider(rpc);
	  let provider = new ethers.providers.Web3Provider(currentProvider);
	  return provider.getTransactionCount(address);
	},
	
	// 同链转账
	ethSendTransaction(chainId, transaction = {}, privateKey) {
	  if (!transaction.to || !transaction.value) {
	  }
	  let rpc = getRpcUrl(chainId);
	  let currentProvider = new Uniprovider(rpc);
	  let provider = new ethers.providers.Web3Provider(currentProvider);
	  let wallet = new ethers.Wallet(privateKey, provider);
	  // Send the transaction
	  return wallet.sendTransaction(transaction)
	},
	
	/**
	 * Get transaction details through hash
	 * @param chainId
	 * @param txHash
	 * @returns {*}
	 */
	ethGetTransactionReceipt(chainId, txHash) {
	  let rpc = getRpcUrl(chainId);
	  let currentProvider = new Uniprovider(rpc);
	  let provider = new ethers.providers.Web3Provider(currentProvider);
	  console.log('-----getTransactionReceipt', txHash)
	  return provider.getTransactionReceipt(txHash)
	},
	
	/**
	 * Call contract
	 * @param contractAbi
	 * @param contractAddress
	 * @param privateKey
	 * @param chainId
	 */
	ethCallContract({ contractAbi, contractAddress, privateKey, chainId }) {
	  let rpc = getRpcUrl(chainId);
	  let currentProvider = new Uniprovider(rpc);
	  let provider = new ethers.providers.Web3Provider(currentProvider);
	  let wallet;
	  if (privateKey) {
	    wallet = new ethers.Wallet(privateKey, provider);
	  }
	  return new ethers.Contract(contractAddress, contractAbi, wallet || provider);
	},
}
